package com.roadmap.struts.action.admin.sns;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.roadmap.client.module.IManager;
import com.roadmap.client.sns.FriendshipManager;
import com.roadmap.client.sns.VisitorManager;
import com.roadmap.common.RdcResourceBundle;
import com.roadmap.struts.action.ActionConst;
import com.roadmap.struts.action.RdcDispatchAction;

public class FriendAction extends RdcDispatchAction {

	@Override
	public ActionForward doView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String ownerId = request
				.getParameter(ActionConst.Aspirant_Id_In_Request);
		// if ownerId got from request URL is invalid, redirect with current
		// one.
		if (ownerId == null || ownerId.trim().length() == 0) {
			return handleInvalidUser(request, "/friend.do?act=view");
		}

		// get the friends
		String groupId = request.getParameter(ActionConst.Parent_Id_In_Request);
		FriendshipManager mgr = new FriendshipManager(ownerId);
		List friends = mgr.getFriendsByGroup(groupId);
		if (friends != null && !friends.isEmpty()) {
			request.setAttribute("friends", friends);
		}

		// put the item owner's name in the request
		boolean isHost = isOwner(request);
		String name = isHost ? RdcResourceBundle.ME_IN_CHINESE : mgr.getOwner()
				.getRealName();
		request.setAttribute(ActionConst.Aspirant_Name_In_Request, name);

		// according to the current user, show the results on corresponding page
		if (isHost) {
			/*// get the friend group
			List groups = mgr.getGroups();
			if (groups != null && !groups.isEmpty()) {
				request.setAttribute("groups", groups);
			}*/
			return mapping.findForward("getfrd");
			
		}
		else {
			request.setAttribute("fromAction", "friend");
			return mapping.findForward("getresult");
		}
	}

	@Override
	public boolean isFormSubmitted(HttpServletRequest request) throws Exception {
		return false;
	}

	public ActionForward visit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionForward forward = getErrorForward(mapping, form, request,
				response);
		if(forward != null) {
			return forward;
		}
		
		String ownerId = request
				.getParameter(ActionConst.Aspirant_Id_In_Request);
		// if not user id was found from url, to get records of the current user
		if(ownerId == null) {
			ownerId = getUIDFromSession(request);
		}
		IManager mgr = new VisitorManager(ownerId);
		List visitors = mgr.getEntitiesOfAspirant();
		if (visitors != null && !visitors.isEmpty()) {
			request.setAttribute("visitors", visitors);
		}
		
		// put the item owner's name in the request
		boolean isHost = isOwner(request);
		String name = isHost ? RdcResourceBundle.ME_IN_CHINESE : mgr.getOwner()
				.getRealName();
		request.setAttribute(ActionConst.Aspirant_Name_In_Request, name);
		
		// which action
		request.setAttribute("fromAction", "visit");
		return mapping.findForward("getresult");
	}

}
